package com.ruoyi.project.modules.fieldEnum.controller;

import java.util.List;

import com.ruoyi.project.modules.fieldEnum.domain.FieldEnumImport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.fieldEnum.domain.FieldEnum;
import com.ruoyi.project.modules.fieldEnum.service.IFieldEnumService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 字段枚举Controller
 *
 * @author smallrain
 * @date 2021-12-19
 */
@Controller
@RequestMapping("/fieldEnum/fieldEnum")
public class FieldEnumController extends BaseController {
    private String prefix = "fieldEnum/fieldEnum";

    @Autowired
    private IFieldEnumService fieldEnumService;

    @RequiresPermissions("fieldEnum:fieldEnum:view")
    @GetMapping()
    public String fieldEnum(ModelMap mmap) {
        List<FieldEnum> fieldList = this.fieldEnumService.fieldList();
        mmap.put("fieldList", fieldList);
        return prefix + "/fieldEnum";
    }

    /**
     * 查询字段枚举列表
     */
    @RequiresPermissions("fieldEnum:fieldEnum:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FieldEnum fieldEnum) {
        startPage();
        List<FieldEnum> list = fieldEnumService.selectFieldEnumList(fieldEnum);
        return getDataTable(list);
    }

    /**
     * 导出字段枚举列表
     */
    @RequiresPermissions("fieldEnum:fieldEnum:export")
    @Log(title = "字段枚举", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FieldEnum fieldEnum) {
        List<FieldEnum> list = fieldEnumService.selectFieldEnumList(fieldEnum);
        ExcelUtil<FieldEnum> util = new ExcelUtil<FieldEnum>(FieldEnum.class);
        return util.exportExcel(list, "字段枚举数据");
    }

    /**
     * 新增字段枚举
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<FieldEnum> fieldList = this.fieldEnumService.fieldList();
        mmap.put("fieldList", fieldList);
        return prefix + "/add";
    }

    /**
     * 新增保存字段枚举
     */
    @RequiresPermissions("fieldEnum:fieldEnum:add")
    @Log(title = "字段枚举", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FieldEnum fieldEnum) {
        return toAjax(fieldEnumService.insertFieldEnum(fieldEnum));
    }

    /**
     * 修改字段枚举
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        FieldEnum fieldEnum = fieldEnumService.selectFieldEnumById(id);
        mmap.put("fieldEnum", fieldEnum);
        List<FieldEnum> fieldList = this.fieldEnumService.fieldList();
        mmap.put("fieldList", fieldList);
        return prefix + "/edit";
    }

    /**
     * 修改保存字段枚举
     */
    @RequiresPermissions("fieldEnum:fieldEnum:edit")
    @Log(title = "字段枚举", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FieldEnum fieldEnum) {
        return toAjax(fieldEnumService.updateFieldEnum(fieldEnum));
    }

    /**
     * 删除字段枚举
     */
    @RequiresPermissions("fieldEnum:fieldEnum:remove")
    @Log(title = "字段枚举", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(fieldEnumService.deleteFieldEnumByIds(ids));
    }

    /**
     * 下载模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<FieldEnumImport> util = new ExcelUtil<>(FieldEnumImport.class);
        return util.importTemplateExcel("字段枚举数据");
    }

    /**
     * 导入数据
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<FieldEnumImport> util = new ExcelUtil<FieldEnumImport>(FieldEnumImport.class);
        List<FieldEnumImport> fieldEnumImportList = util.importExcel(file.getInputStream());
        String message = fieldEnumService.importData(fieldEnumImportList);
        return AjaxResult.success(message);
    }
}
